package com.eboss2.sbs.activity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ScrollView;

import com.eboss2.sbs.R;
import com.eboss2.sbs.activity.base.TabBasicActivity;
import com.eboss2.sbs.config.Config;
import com.eboss2.sbs.http.HttpUtil;
import com.eboss2.sbs.myapplication.MyApplication;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshScrollView;
import com.loopj.android.http.RequestParams;
import com.mytools.tool.activityManager.MyActivityManager;
import com.tool.utils.utils.StringUtils;
import com.tool.utils.utils.ToastUtils;

import org.json.JSONObject;

public class MainActivity extends TabBasicActivity implements View.OnClickListener{
	private final String TAG = "MainActivity";
	private PullToRefreshScrollView scroolView;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mContext = this;
		setContentView(R.layout.activity_main);
		MyActivityManager.getInstance().addActivity(this);
//		StatusBarUtil.setColor(this, getResources().getColor(R.color.statusBar), 0);
		initView();
		
		//百度定位
		//mApp.locationClient.start();

//		BdTts.getInstance(mContext).speak("大幅度鼎折覆餗");
		
		findViewById(R.id.layout_point_activate).setOnClickListener(this);
		findViewById(R.id.layout_coupon_activate).setOnClickListener(this);
		findViewById(R.id.layout_coupon).setOnClickListener(this);
		findViewById(R.id.layout_consumption).setOnClickListener(this);
		findViewById(R.id.layout_transaction).setOnClickListener(this);
		findViewById(R.id.layout_point_verification).setOnClickListener(this);
		findViewById(R.id.layout_coupon_verification).setOnClickListener(this);
		
		//网络请求
//		getdata();
		
	}
	
	@SuppressWarnings("deprecation")
	private void initView(){
		showView(R.id.titleback_btn_back, false);
		setTvText(R.id.tv_title, "首页");
		scroolView = (PullToRefreshScrollView) findViewById(R.id.scroolView);
		scroolView.setMode(PullToRefreshBase.Mode.PULL_DOWN_TO_REFRESH);
		scroolView.setOnRefreshListener(new PullToRefreshViewEvent()); //下拉刷新 上拉加载
	}

	@Override
	protected void onResume() {
		super.onResume();
		getdata();
	}

	private void getdata(){

		showProgress("加载中...");
		RequestParams params = new RequestParams();
		params.add("shopId", MyApplication.getmInstance().getLoginMEntity().getShopid()+"");
		HttpUtil.get(Config.SBS_INDEX, params, new HttpUtil.RequestListener() {
			@Override
			public void success(String response) {
				scroolView.onRefreshComplete();
				disShowProgress();
				try {
					JSONObject result = new JSONObject(response);

					if (!result.optBoolean("status")) {
						ToastUtils.CustomShow(mContext, result.optString("message"));
					}else{
						JSONObject dataStr = result.getJSONObject("data");
						int yestBonusSendAmount = dataStr.optInt("yestBonusSendAmount");
						int veriftyCouponNumber = dataStr.optInt("veriftyCouponNumber");
						int veriftyBonusAmount = dataStr.optInt("veriftyBonusAmount");
						int yestOrderNum = dataStr.optInt("yestOrderNum");
						int yestTradeAmount = dataStr.optInt("yestTradeAmount");
						int todayTradeAmount = dataStr.optInt("todayTradeAmount");
						int yestcouponSendNumber = dataStr.optInt("yestcouponSendNumber");
						int todayOrderNum = dataStr.optInt("todayOrderNum");


						setTvText(R.id.tv_shopMemberYesterday, yestOrderNum+"笔");
						setTvText(R.id.tv_shopMemberToday, todayOrderNum+"笔");
						setTvText(R.id.tv_turnoverYesterday, StringUtils.formatIntMoney(yestTradeAmount)+"元");
						setTvText(R.id.tv_turnoverToday, StringUtils.formatIntMoney(todayTradeAmount)+"元");
						setTvText(R.id.tv_pointIssueYesterday, yestBonusSendAmount+"点");
						setTvText(R.id.tv_pointVerificatYesterday, veriftyBonusAmount+"点");
						setTvText(R.id.tv_couponIssueYesterday, yestcouponSendNumber+"张");
						setTvText(R.id.tv_couponVerificatYesterday, veriftyCouponNumber+"张");

					}



//


				} catch (Exception e) {
					Log.e(TAG, "doLogin() Exception: " + e.getMessage());
				}


			}

			@Override
			public void failed(Throwable error) {
				scroolView.onRefreshComplete();
				disShowProgress();
				showE404();
			}
		});


//		Map<String, Object> params = new HashMap<String, Object>();
////		params.put("sid", mApp.getLoginEntity().getSid());
//		API.http_post(this, API.HOMEPAGE, params, new HttpCallback<HomeEntity>(this) {
//			@Override
//			public void onSuccess(int statusCode, HomeEntity data) {
//				scroolView.onRefreshComplete();
//				setTvText(R.id.tv_shopMemberYesterday, data.getShopMemberYesterday()+"人");
//				setTvText(R.id.tv_shopMemberToday, data.getShopMemberToday()+"人");
//				setTvText(R.id.tv_turnoverYesterday, StringUtil.getMoneyString(data.getTurnoverYesterday())+"元");
//				setTvText(R.id.tv_turnoverToday, StringUtil.getMoneyString(data.getTurnoverToday())+"元");
//				setTvText(R.id.tv_pointIssueYesterday, data.getPointIssueYesterday()+"点");
//				setTvText(R.id.tv_pointVerificatYesterday, data.getPointVerificatYesterday()+"点");
//				setTvText(R.id.tv_couponIssueYesterday, data.getCouponIssueYesterday()+"张");
//				setTvText(R.id.tv_couponVerificatYesterday, data.getCouponVerificatYesterday()+"张");
//				//scan();
//			}
//
//			@Override
//			public void onFailure(int statusCode, String message) {
//				scroolView.onRefreshComplete();
//				super.onFailure(statusCode, message);
//			}
//
//			@SuppressWarnings({ "unchecked", "rawtypes" })
//			@Override
//			public TypeToken getTypeToken() {
//				return new TypeToken<HomeEntity>() {
//				};
//			}
//		});
	}
	
	//下拉刷新 上拉加载
	public class PullToRefreshViewEvent implements PullToRefreshBase.OnRefreshListener<ScrollView> {
		@Override
		public void onRefresh(PullToRefreshBase<ScrollView> refreshView) {
			getdata();
		}
	}
	
//	@Override
//	public void onReceiveLocation(BDLocation arg0) {
//		Toast.makeText(MainActivity.this, arg0.getCity(), Toast.LENGTH_SHORT).show();
//		super.onReceiveLocation(arg0);
//	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.layout_point_activate:
				ToastUtils.CustomShow(mContext, "该功能开发中。。。");

				break;
			case R.id.layout_coupon_activate:
				ToastUtils.CustomShow(mContext, "该功能开发中。。。");
				break;
			case R.id.layout_coupon:
				ToastUtils.CustomShow(mContext, "该功能开发中。。。");
				break;
			case R.id.layout_consumption:
				ToastUtils.CustomShow(mContext, "该功能开发中。。。");
				break;
			case R.id.layout_transaction:
				ToastUtils.CustomShow(mContext, "该功能开发中。。。");
				break;
			case R.id.layout_point_verification:
				ToastUtils.CustomShow(mContext, "该功能开发中。。。");
				break;
			case R.id.layout_coupon_verification:
				ToastUtils.CustomShow(mContext, "该功能开发中。。。");
				break;
			default:break;
		}
	}
	
}
